import { Options } from '@antv/x6'

export const config: Options.Manual = {
  autoResize: true,
  grid: {
    visible: true,
    type: 'doubleMesh',
    args: [
      {
        color: '#eee', // 主网格线颜色
        thickness: 1 // 主网格线宽度
      },
      {
        color: '#ddd', // 次网格线颜色
        thickness: 1, // 次网格线宽度
        factor: 4 // 主次网格线间隔
      }
    ]
  },
  background: {
    color: '#F2F7FA'
  },
  mousewheel: {
    enabled: true,
    modifiers: 'Ctrl',
    maxScale: 4,
    minScale: 0.2
  },
  panning: true,
  connecting: {
    allowEdge: false,
    allowBlank: false,
    allowMulti: true,
    allowLoop: false,
    allowNode: false,
    router: 'normal',
    validateMagnet({ e, magnet, view, cell }) {
      //console.log(magnet,magnet.getAttribute('x-type'))
      return magnet.getAttribute('x-type')?.startsWith('output-') || false
    },
    validateConnection({
      edge,
      edgeView,
      sourceView,
      targetView,
      sourcePort,
      targetPort,
      sourceMagnet,
      targetMagnet,
      sourceCell,
      targetCell,
      type
    }) {
      //console.log(sourceMagnet,targetMagnet)
      //console.log(sourceCell,targetCell)
      const sourceType = sourceMagnet?.getAttribute('x-type')?.replace('output-', '')
      const targetType = targetMagnet?.getAttribute('x-type')?.replace('input-', '')
      const canLink =
        (sourceType === targetType)
        ||(sourceType==='intention' && targetType==='exec')
        ||(targetType==='any' && sourceType!=='exec'&& sourceType!=='intention')
        ||(sourceType==='string' && targetType==='text')

      console.log('canLink', canLink)
      if (canLink && sourceMagnet && targetMagnet && sourceCell && targetCell) {
        //写入使用
        const from = sourceMagnet.getAttribute('x-id') as string
        const to = targetMagnet.getAttribute('x-id') as string
        const key = sourceMagnet.getAttribute('x-key') as string;
        const op = sourceMagnet.getAttribute('x-op') as string;
        const alias =targetMagnet.getAttribute("x-alias") as string;
        const source = sourceCell.id
        const target = targetCell.id
        if (edge) {
          edge.setData({ type: sourceType,targetType, from, to,source,target,key,alias,op})
          if (sourceType !== 'exec'&& sourceType !== 'intention') {
            edge?.setAttrs({
              line: {
                stroke: '#dddddd',
                sourceMarker: {
                  name:'circle',
                  r:3
                },
                targetMarker: {
                  name:'circle',
                  r:3
                },
              }
            })
          }else {
            edge?.setAttrs({
              line: {
                stroke: '#0675f1'
              }
            })
          }
        }
      }
      return canLink
    }
  },
}
